<?

require ("class.calendar.inc");

if ($_REQUEST["cal_option"]) $_SESSION["cal_option"] = $_REQUEST["cal_option"];
else $_SESSION["cal_option"] = "wk_view";

function display_calendar ($options) {

	$_APP_CALENDAR = new Container ();
	$_APP_CALENDAR->set_variable ("options", $options);
	$_APP_CALENDAR->set_option ("callback_end", true);

	if ($_SESSION["cal_option"] == "dy_view")
		$_APP_CALENDAR->set_callback_begin ("calendar_dy_view");
	elseif ($_SESSION["cal_option"] == "wk_view")
		$_APP_CALENDAR->set_callback_begin ("calendar_wk_view");
	elseif ($_SESSION["cal_option"] == "mo_view")
		$_APP_CALENDAR->set_callback_begin ("calendar_mo_view");
	elseif ($_SESSION["cal_option"] == "yr_view")
		$_APP_CALENDAR->set_callback_begin ("calendar_yr_view");

	return $_APP_CALENDAR;
}

function calendar_dy_view ($container) {

	$options = $container->get_variable ("options");
}

function calendar_wk_view ($container) {

	$options = $container->get_variable ("options");
	$calendar = new Calendar($options);

	if ($_REQUEST["week_scroll"])
			$_SESSION["week_scroll"] = $_REQUEST["week_scroll"];
	elseif (!$_REQUEST["week_scroll"]) $_SESSION["week_scroll"] = 0;

	$wk_prev = $_SESSION["week_scroll"] - 1;
	$wk_next = $_SESSION["week_scroll"] + 1;

	$week = $calendar->create_week ($_SESSION["week_scroll"]);
	$current_month = $calendar->get_month () .
			" :: " . $calendar->get_year ();

	print <<<HTML
<table class="css_calendar" border="0" cellpadding="0" cellspacing="1" width="100%">
HTML;

	if (!isset ($options["no_add_entry"])) {

		print <<<HTML
<tr>
	<td colspan="7" align="right">

	<a class="css_cal_action" href="?{$options["link_options"]}{$options["add_entry_link"]}timestamp={$_REQUEST["timestamp"]}"><div class="css_cal_entry">Add Calendar Entry</div></a></button>

</tr>
HTML;
	}

	print <<<HTML
<tr>
	<td colspan="7" align="center">

<table cellpadding="0" cellspacing="0" width="100%">
<tr class="css_cal_navigation"><td class="css_cal_view" align="right" colspan="3"><a class="css_cal_view_link" href="?{$options["link_options"]}cal_option=yr_view"><div class="css_cal_label_year">Year View</div></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a class="css_cal_view_link" href="?{$options["link_options"]}cal_option=mo_view"><div class="css_cal_label_month">Month View</div></a></td></tr>
<tr class="css_cal_navigation">
	<td align="left">&nbsp;&nbsp;<a class="css_cal_navigation" href="?{$options["link_options"]}week_scroll={$wk_prev}"><div class="css_cal_prev">Prev</div></a></td>
	<td class="css_cal_navigation" align="center">{$current_month}</td>
	<td align="right"><a class="css_cal_navigation" href="?{$options["link_options"]}week_scroll={$wk_next}"><div class="css_cal_next">Next</div></a>&nbsp;&nbsp;</td>
</tr>
</table>

	</td>
</tr>
<tr>

HTML;

	for ($d = 0; $d < 5; $d++) {

		print <<<HTML
<td class="css_day_title">{$calendar->wkdys[$d][1]}</td>
HTML;
	}

	print <<<HTML

</tr>

HTML;

	print <<<HTML
HTML;

	while ($day = $week->iterate_days ()) {

		if ($day->cmo) $class="css_mo_day_b";
		else $class="css_mo_day_a";
		if ($day->cmo && $calendar->today == $day->begin)
				$class = "css_mo_day_c";
		$mday = $day->get_day();
		if ($options["action"]) $action = "&" . $options["action"];

		print <<<HTML
	<td class="{$class}" width="50" height="135"><a class="{$class}" href="?{$options["link_options"]}timestamp={$day->begin}{$action}"><div class="css_mo_day_a">{$mday}</div></a>

HTML;

		if (isset ($options["callback_begin"]))
			$options["callback_begin"]($day, $week, $month, $calendar);
		if (isset ($options["callback"]))
			$options["callback"]($day, $week, $month, $calendar);
		if (isset ($options["callback_end"]))
			$options["callback_end"]($day, $week, $month, $calendar);

		print <<<HTML
	</td>

HTML;

		if (++$i == 5) {

			print <<<HTML
</tr>
<tr>
	<td class="css_cal_cell" colspan="3" rowspan="2">
HTML;

			calendar_mo_load ($options);	

			print <<<HTML
	</td>
HTML;

			for ($d = 5; $d < 7; $d++) {

				print <<<HTML
<td class="css_day_title">{$calendar->wkdys[$d][1]}</td>
HTML;
			}

			print <<<HTML

</tr>

HTML;
		}
	}

	print <<<HTML
</tr>
</table>

HTML;

}

function calendar_mo_view ($container) {

	$options = $container->get_variable ("options");
	$options["view_calendar"] = true;

	calendar_mo_load ($options);	
}

function calendar_mo_load ($options, $mo = array ()) {

	$calendar = new Calendar($options);

	if ($_REQUEST["month_scroll"]) $_SESSION["month_scroll"] = $_REQUEST["month_scroll"];
	elseif (!$_REQUEST["month_scroll"])  $_SESSION["month_scroll"] = 0;

	$mo_prev = $_SESSION["month_scroll"] - 1;
	$mo_next = $_SESSION["month_scroll"] + 1;

	if ($options["view_calendar"]) $view_calendar = "&cal_option=mo_view";

	$month = $calendar->create_month (
			count ($mo) ? $mo["value"] : $_SESSION["month_scroll"]);
	$current_month = $calendar->get_month () .
			" :: " . $calendar->get_year ();
 	if ($_SESSION["cal_option"] == "wk_view") $class = "css_day_title";
	else $class = "css_cal_view";

	print <<<HTML
<table border="0" cellpadding="0" cellspacing="1" width="100%">
<tr>
	<td colspan="7" align="center">

<table cellpadding="0" cellspacing="0" width="100%">
HTML;

	if ($_REQUEST["cal_option"] == "mo_view")

		print <<<HTML
<tr class="css_cal_navigation"><td align="right" class="{$class}" colspan="3"><a class="css_cal_view_link" href="?{$options["link_options"]}timestamp={$calendar->month->weeks[0]->begin}&cal_option=wk_view"><div class="css_cal_label_week">Week View</div></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a class="css_cal_view_link" href="?{$options["link_options"]}cal_option=yr_view"><div class="css_cal_label_year">Year View</div></a></td></tr>
HTML;

	print <<<HTML
<tr class="css_cal_navigation">
	<td align="left">&nbsp;&nbsp;<a class="css_cal_navigation" href="?{$options["link_options"]}month_scroll={$mo_prev}{$view_calendar}"><div class="css_cal_prev">Prev</div></a></td>
	<td class="css_cal_navigation" align="center">{$current_month}</td>
	<td align="right"><a class="css_cal_navigation" href="?{$options["link_options"]}month_scroll={$mo_next}{$view_calendar}"><div class="css_cal_next">Next</div></a>&nbsp;&nbsp;</td>
</tr>
</table>

	</td>
</tr>
<tr>

HTML;

	foreach ($calendar->wkdys as $day) {

		print <<<HTML
<td class="css_day_title">{$day[1]}</td>
HTML;
	}
		print <<<HTML
</tr>

HTML;
	while ($week = $month->iterate_weeks ()) {

		print <<<HTML
<tr>

HTML;

		if ($_SESSION["cal_option"] == "mo_view" && !$options["small"]) {

			$height = " height=\"50\"";
		}

		$width = " width=\"13%\"";

		while ($day = $week->iterate_days ()) {

			if ($day->cmo) {
			
				if ($week->current) $class = "css_mo_day_c";
				else $class = "css_mo_day_b";
			} else {
				if ($week->current) {
					if ($day->cmo) $class = "css_mo_day_c";
					else $class = "css_mo_day_e";
				} else $class = "css_mo_day_a";
			}
			if ($day->cmo && $calendar->today == $day->begin)
					$class = "css_mo_day_d";

			if ($options["action"])
				$action = "&" . $options["action"];

			$mday = $day->get_day();
			print <<<HTML
	<td class="{$class}"{$height}{$width}><a class="{$class}" href="?{$options["link_options"]}timestamp={$day->begin}{$action}"><div>{$mday}</div></a></td>

HTML;
		}

		print <<<HTML
</tr>

HTML;
	}

	print <<<HTML
</table>
HTML;
}

function calendar_yr_view ($container) {

	$options = $container->get_variable ("options");
	$today = localtime ($options["timestamp"]);
	$year = $today[5] + 1900;
	$month = $today[4];
	$months = array ();

	if ($_REQUEST["year_scroll"])
			$_SESSION["year_scroll"] = $_REQUEST["year_scroll"];

	$calendar = new Calendar ($options);

	for ($i = $month; $i > 0; --$i)
			array_push ($months, -1 * $i);
	for ($i = $month; $i < 12; $i++)
			array_push ($months, $i - $month);

	print <<<HTML
<table class="css_calendar" border="0" cellspacing="1" cellpadding="0" width="100%">
<tr>
	<td colspan="7" align="center">

<table cellpadding="0" cellspacing="0" width="100%">
<tr class="css_cal_navigation"><td class="css_cal_view" align="right" colspan="3"><a class="css_cal_view_link" href="?{$options["link_options"]}cal_option=wk_view"><div class="css_cal_label_week">Week View</div></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a class="css_cal_view_link" href="?{$options["link_options"]}cal_option=mo_view"><div class="css_cal_label_month">Month View</div></a></td></tr>
<tr class="css_cal_navigation">
	<td align="left">&nbsp;&nbsp;<a class="css_cal_navigation" href="?{$options["link_options"]}week_scroll={$wk_prev}"><div class="css_cal_prev">Prev</div></a></td>
	<td class="css_cal_navigation" align="center">{$current_month}</td>
	<td align="right"><a class="css_cal_navigation" href="?{$options["link_options"]}week_scroll={$wk_next}"><div class="css_cal_next">Next</div></a>&nbsp;&nbsp;</td>
</tr>
</table>

	</td>
</tr>

HTML;
	for ($i = 0; $i < count ($months); $i+=2) {

		print <<<HTML
<tr><td>

HTML;
		calendar_mo_load ($options, array (value => $months[$i]));

		print <<<HTML
</td><td>

HTML;

		calendar_mo_load ($options, array (value => $months[$i+1]));

		print <<<HTML
<td></tr>

HTML;
	}

	print <<<HTML
</table>

HTML;
}

?>
